﻿using System.Collections.Generic;
using winS.UnityEditor.Undo;

namespace winS.UnityEditor.ProcessEditor
{
    public class PasteNodeCommand : IUndoCommand
    {
        public string info => "粘贴节点";

        private readonly StepGraphView stepGraphView;
        private readonly IEnumerable<StepNode> stepNodes;
        private readonly IEnumerable<ConnectionInfo> connectionInfos;

        public PasteNodeCommand(IEnumerable<StepNode> stepNodes, IEnumerable<ConnectionInfo> connectionInfos, StepGraphView stepGraphView)
        {
            this.stepNodes = stepNodes;
            this.stepGraphView = stepGraphView;
            this.connectionInfos = connectionInfos;
        }

        public void Undo()
        {
            foreach (var stepNode in stepNodes) stepGraphView.RemoveNode(stepNode);
            foreach (var connectionInfo in connectionInfos)
            {
                Connectionline connectionline = connectionInfo.connectionline;
                connectionline.Disconnect();
                stepGraphView.RemoveConnectionline(connectionline);
            }
        }
        public void Redo()
        {
            foreach (var stepNode in stepNodes) stepGraphView.AddNode(stepNode);
            foreach (var connectionInfo in connectionInfos)
            {
                Connectionline connectionLine = connectionInfo.connectionline;
                connectionLine.Connect(connectionInfo.inputPort, connectionInfo.outputPort);
                stepGraphView.AddConnectionline(connectionLine);
            }
        }
    }
}